package cn.xgj.sfzy.service.impl;

import cn.xgj.sfzy.entity.Consult;
import cn.xgj.sfzy.entity.Reply;
import cn.xgj.sfzy.mapper.ConsultMapper;
import cn.xgj.sfzy.mapper.ReplyMapper;
import cn.xgj.sfzy.service.ReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName ReplyServiceImpl
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月24日 上午10:56
 * @Version 1.0
 */
@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private ConsultMapper consultMapper;


    @Override
    public int add(Reply reply) {
        return replyMapper.insert(reply);
    }

    @Override
    public int edit(Reply reply) {
        return replyMapper.update(reply);
    }

    @Override
    public int remove(Integer id) {
        return replyMapper.delete(id);
    }

    @Override
    public List<Reply> findAll(Reply reply) {
        return replyMapper.find(reply);
    }

    @Override
    public List<Reply> findByQuestionId(Integer id) {
        List<Reply> list = replyMapper.findByQuestionId(id);
        for (Reply r : list) {
            Long consultId = r.getConsultId();
            Consult consult = consultMapper.selectById(consultId.intValue());
            r.setConsult(consult);
        }
        return list;
    }

    @Override
    public Reply findById(Integer id) {
        Reply r = replyMapper.findById(id);
        Long consultId = r.getConsultId();
        Consult consult = consultMapper.selectById(consultId.intValue());
        r.setConsult(consult);
        return r;
    }

    @Override
    public PageInfo<Reply> findByPage(Integer pageNum, Integer ageSize, Reply reply) {
        PageHelper.startPage(pageNum, ageSize);
        List<Reply> list = replyMapper.find(reply);
        for (Reply r : list) {
            Long consultId = r.getConsultId();
            Consult consult = consultMapper.selectById(consultId.intValue());
            r.setConsult(consult);
        }
        PageInfo<Reply> pageInfo = new PageInfo<Reply>(list);
        return pageInfo;
    }
}
